package com.br.mercarad.uteis;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;

import com.br.mercarad.projeto.ProjetoVO;

public class Utils {
	
	private String dirbase;
	
	public Utils() {
		Path path = Paths.get(System.getProperty("user.dir"));
		this.dirbase = path.getParent() + "\\MercanetAndroid\\";		
	}

	
	public String getDirbase() {
		return dirbase;
	}

	public void setDirbase(String dirbase) {
		this.dirbase = dirbase;
	}

	public void copyStringToClipboard(String str) {
		StringSelection stringSelection = new StringSelection(str);
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		clipboard.setContents(stringSelection, null);
	}

	public String openFile(String pathFile) {
		 StringBuilder strB = new StringBuilder();
		try {
			String fileName = pathFile;
			if (pathFile.indexOf(dirbase)<0)
				fileName = dirbase + pathFile;

			BufferedReader in = new BufferedReader(new FileReader(fileName));

			String str;
			while ((str = in.readLine()) != null) {
				strB.append(str + "\n");
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strB.toString();
	}

	public List<String> findTextInFile(String directory, List<String> filenames, Boolean recursive, String searchStr,String pattern) {
	//public void findTextInFile(String string, String nome, boolean b) {
		
		
		//public void listAllFiles(String directory, ProjetoVO parrentVO, Boolean recursive) {
			File [] children = new File(directory).listFiles(); // list all the files in the directory
			//ProjetoVO projeto;
			for (int i = 0; i < children.length; i++) { // loop through each
//				projeto = new ProjetoVO();
				//projeto.setParrentVO(parrentVO);
				
//				projeto.setNomeTela(children[i].getName()); 
//				parrentVO.addChild(projeto);
				
				if (children[i].isDirectory() && recursive) { 
//					projeto.setTipo(1);
					filenames = findTextInFile(children[i].getPath(),filenames, recursive, searchStr,pattern); // call again for the subdirectory
				} else if (!children[i].isDirectory()){ // otherwise, if it isn't a directory
					if (children[i].getName().toLowerCase().contains(searchStr.toLowerCase().substring(0,searchStr.length()-1)+pattern)){
						filenames.add(children[i].getPath());
					}
				}
				
			}
			return filenames; 
		}
	
			
	private boolean find(File f, String searchString) {
		boolean result = false;
		Scanner in = null;
		try {
			in = new Scanner(new FileReader(f));
			while (in.hasNextLine() && !result) {
				result = in.nextLine().indexOf(searchString) >= 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (Exception e) { /* ignore */
			}
		}
		return result;
	}
}
